from datetime import datetime
from typing import Optional, Union

from pydantic import BaseModel


class BlogElastic(BaseModel):
    id: int
    title: str
    content: str
    category: str
    createTime: Optional[Union[datetime, str]] = None
    updateTime: Optional[Union[datetime, str]] = None

    class Config:
        from_attributes = True


class BlogSearch(BaseModel):
    id: int
    title: str
    content: str
    highlight: str

    class Config:
        from_attributes = True


BLOG_MAPPING = {
    "mappings": {
        "properties": {
            "id": {"type": "long"},
            "title": {
                "type": "text",
                "analyzer": "ik_max_word",
                "fields": {
                    "keyword": {"type": "keyword"}
                }
            },
            "content": {
                "type": "text",
                "analyzer": "ik_max_word"
            },
            "category": {
                "type": "keyword"
            },
            "createTime": {
                "type": "date",
                "format": "yyyy-MM-dd HH:mm:ss||epoch_millis"
            },
            "updateTime": {
                "type": "date",
                "format": "yyyy-MM-dd HH:mm:ss||epoch_millis"
            }
        }
    },
    "settings": {
        "number_of_shards": 1,
        "number_of_replicas": 1,
        "analysis": {
            "analyzer": {
                "ik_max_word": {
                    "type": "ik_max_word"
                }
            }
        }
    }
}
